// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@

package example;

import java.awt.*;
import java.util.Optional;
import javax.swing.*;

public final class MainPanel extends JPanel {
  private MainPanel() {
    super(new GridLayout(1, 2));
    // UIManager.put("ScrollBar.alwaysShowThumb", Boolean.TRUE);
    JScrollPane scroll = new JScrollPane(new JTable(24, 3));
    // scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    scroll.getVerticalScrollBar().addAdjustmentListener(e -> {
      JScrollBar scrollBar = (JScrollBar) e.getAdjustable();
      BoundedRangeModel m = scrollBar.getModel();
      int value = m.getValue();
      boolean max = value == m.getMaximum() - m.getExtent();
      Optional.ofNullable(scrollBar.getComponent(0)).ifPresent(b -> b.setEnabled(!max));
      boolean min = value == m.getMinimum();
      Optional.ofNullable(scrollBar.getComponent(1)).ifPresent(b -> b.setEnabled(!min));
    });
    add(new JScrollPane(new JTable(24, 3)));
    add(scroll);
    setPreferredSize(new Dimension(320, 240));
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(MainPanel::createAndShowGui);
  }

  private static void createAndShowGui() {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (UnsupportedLookAndFeelException ignored) {
      Toolkit.getDefaultToolkit().beep();
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
      ex.printStackTrace();
      return;
    }
    JFrame frame = new JFrame("@title@");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.getContentPane().add(new MainPanel());
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}
